unit Class_ZBD_FL;

interface
uses
  Classes,SysUtils,Uni,UniEngine;

type
  TZBDFL=class(TUniEngine)
  private
    FZBDFLBH  : Integer;
    FZBDBH    : string;
    FZBDDWBML : string;            
    FZBDFLZJLX: string;
    FZBDFLZCLX: string;
    FZBDFLBJKM: string;
    FZBDFLXJKM: string;
    FZBDFLJE  : Extended;
    FZBDFLBZ  : string;
    FZBDFLJY  : Extended;
    FZBDFLZBLY: string;
    FZBDFLDJLX: string;
  protected
    procedure SetParameters;override;
    function  GetStrInsert:string;override;
    function  GetStrUpdate:string;override;
    function  GetStrDelete:string;override;
  public
    function  GetStrsIndex:string;override;
  public
    function  GetNextIdex:Integer;overload;
    function  GetNextIdex(AUniConnection:TUniConnection):Integer;overload;
  public
    function  CheckExist(AUniConnection:TUniConnection):Boolean;override;
  public
    destructor  Destroy; override;
    constructor Create;  
  published
    property ZBDFLBH  : Integer read FZBDFLBH   write FZBDFLBH;
    property ZBDBH    : string read FZBDBH     write FZBDBH;
    property ZBDDWBML : string read FZBDDWBML  write FZBDDWBML;
    property ZBDFLZJLX: string read FZBDFLZJLX write FZBDFLZJLX;
    property ZBDFLZCLX: string read FZBDFLZCLX write FZBDFLZCLX;
    property ZBDFLBJKM: string read FZBDFLBJKM write FZBDFLBJKM;
    property ZBDFLXJKM: string read FZBDFLXJKM write FZBDFLXJKM;
    property ZBDFLJE  : Extended read FZBDFLJE   write FZBDFLJE;
    property ZBDFLBZ  : string read FZBDFLBZ   write FZBDFLBZ;
    property ZBDFLJY  : Extended read FZBDFLJY   write FZBDFLJY;
    property ZBDFLZBLY: string read FZBDFLZBLY write FZBDFLZBLY;
    property ZBDFLDJLX: string read FZBDFLDJLX write FZBDFLDJLX;    
  public
    class function  ReadDS(AUniQuery:TUniQuery):TUniEngine;override;
    class procedure ReadDS(AUniQuery:TUniQuery;var Result:TUniEngine);override;
    class function  CopyIt(AZBDFL:TZBDFL):TZBDFL;overload;        
    class procedure CopyIt(AZBDFL:TZBDFL;var Result:TZBDFL);overload;

    class function  ExpSQL(AUnitLink:string;ADJLX:string):string;      
  end;

implementation

{ TZBDFL }
procedure TZBDFL.SetParameters;
begin
  inherited;
  with FUniSQL.Params do
  begin
    case FOptTyp of
      otAddx:
      begin
        ParamByName('ZBD_FL_BH').Value := ZBDFLBH;
        ParamByName('ZBD_BH').Value := ZBDBH;
        ParamByName('ZBD_DWBML').Value := ZBDDWBML;
        ParamByName('ZBD_FL_ZJLX').Value := ZBDFLZJLX;
        ParamByName('ZBD_FL_ZCLX').Value := ZBDFLZCLX;
        ParamByName('ZBD_FL_BJKM').Value := ZBDFLBJKM;
        ParamByName('ZBD_FL_XJKM').Value := ZBDFLXJKM;
        ParamByName('ZBD_FL_JE').Value := ZBDFLJE;
        ParamByName('ZBD_FL_BZ').Value := ZBDFLBZ;
        ParamByName('ZBD_FL_JY').Value := ZBDFLJY;
        ParamByName('ZBD_FL_ZBLY').Value := ZBDFLZBLY;
        ParamByName('ZBD_FL_DJLX').Value := ZBDFLDJLX; 
      end;
      otEdit:
      begin
        ParamByName('ZBD_FL_BH').Value := ZBDFLBH;
        ParamByName('ZBD_BH').Value := ZBDBH;
        ParamByName('ZBD_DWBML').Value := ZBDDWBML;
        ParamByName('ZBD_FL_ZJLX').Value := ZBDFLZJLX;
        ParamByName('ZBD_FL_ZCLX').Value := ZBDFLZCLX;
        ParamByName('ZBD_FL_BJKM').Value := ZBDFLBJKM;
        ParamByName('ZBD_FL_XJKM').Value := ZBDFLXJKM;
        ParamByName('ZBD_FL_JE').Value := ZBDFLJE;
        ParamByName('ZBD_FL_BZ').Value := ZBDFLBZ;
        ParamByName('ZBD_FL_JY').Value := ZBDFLJY;
        ParamByName('ZBD_FL_ZBLY').Value := ZBDFLZBLY;
        ParamByName('ZBD_FL_DJLX').Value := ZBDFLDJLX; 
      end;  
      otDelt:
      begin
        ParamByName('ZBD_FL_BH').Value := ZBDFLBH;
        ParamByName('ZBD_BH').Value := ZBDBH;
 
      end;  
    end;
  end;
end;

function TZBDFL.CheckExist(AUniConnection: TUniConnection): Boolean;
begin
  Result:=CheckExist('TBL_ZBD_FL',['ZBD_FL_BH',ZBDFLBH,'ZBD_BH',ZBDBH],AUniConnection);
end;

function TZBDFL.GetNextIdex: Integer;
begin

end;

function TZBDFL.GetNextIdex(AUniConnection: TUniConnection): Integer;
begin

end;

function TZBDFL.GetStrDelete: string;
begin
  Result:='DELETE FROM TBL_ZBD_FL WHERE   ZBD_FL_BH=:ZBD_FL_BH AND ZBD_BH=:ZBD_BH';
end;

function TZBDFL.GetStrInsert: string;
begin
  Result:='INSERT INTO TBL_ZBD_FL'
         +'    ( ZBD_FL_BH, ZBD_BH, ZBD_DWBML, ZBD_FL_ZJLX, ZBD_FL_ZCLX'
         +'    , ZBD_FL_BJKM, ZBD_FL_XJKM, ZBD_FL_JE, ZBD_FL_BZ, ZBD_FL_JY'
         +'    , ZBD_FL_ZBLY, ZBD_FL_DJLX)'
         +'    VALUES'
         +'    (:ZBD_FL_BH,:ZBD_BH,:ZBD_DWBML,:ZBD_FL_ZJLX,:ZBD_FL_ZCLX'
         +'    ,:ZBD_FL_BJKM,:ZBD_FL_XJKM,:ZBD_FL_JE,:ZBD_FL_BZ,:ZBD_FL_JY'
         +'    ,:ZBD_FL_ZBLY,:ZBD_FL_DJLX)';
end;

function TZBDFL.GetStrsIndex: string;
begin
  Result:=Format('%D-%S',[ZBDFLBH,ZBDBH]);
end;

function TZBDFL.GetStrUpdate: string;
begin
  Result:='UPDATE  TBL_ZBD_FL SET'
         +'    ZBD_DWBML=:ZBD_DWBML,'
         +'    ZBD_FL_ZJLX=:ZBD_FL_ZJLX,'
         +'    ZBD_FL_ZCLX=:ZBD_FL_ZCLX,'
         +'    ZBD_FL_BJKM=:ZBD_FL_BJKM,'
         +'    ZBD_FL_XJKM=:ZBD_FL_XJKM,'
         +'    ZBD_FL_JE=:ZBD_FL_JE,'
         +'    ZBD_FL_BZ=:ZBD_FL_BZ,'
         +'    ZBD_FL_JY=:ZBD_FL_JY,'
         +'    ZBD_FL_ZBLY=:ZBD_FL_ZBLY,'
         +'    ZBD_FL_DJLX=:ZBD_FL_DJLX'
         +'    WHERE ZBD_FL_BH=:ZBD_FL_BH'
         +'    AND   ZBD_BH=:ZBD_BH';
end;

constructor TZBDFL.Create;
begin

end;

destructor TZBDFL.Destroy;
begin

  inherited;
end;

class function TZBDFL.ReadDS(AUniQuery: TUniQuery): TUniEngine;
begin
  Result:=TZBDFL.Create;
  with TZBDFL(Result) do
  begin
    ZBDFLBH  :=AUniQuery.FieldByName('ZBD_FL_BH').AsInteger;
    ZBDBH    :=AUniQuery.FieldByName('ZBD_BH').AsString;
    ZBDDWBML :=AUniQuery.FieldByName('ZBD_DWBML').AsString;
    ZBDFLZJLX:=AUniQuery.FieldByName('ZBD_FL_ZJLX').AsString;
    ZBDFLZCLX:=AUniQuery.FieldByName('ZBD_FL_ZCLX').AsString;
    ZBDFLBJKM:=AUniQuery.FieldByName('ZBD_FL_BJKM').AsString;
    ZBDFLXJKM:=AUniQuery.FieldByName('ZBD_FL_XJKM').AsString;
    ZBDFLJE  :=AUniQuery.FieldByName('ZBD_FL_JE').AsFloat;
    ZBDFLBZ  :=AUniQuery.FieldByName('ZBD_FL_BZ').AsString;
    ZBDFLJY  :=AUniQuery.FieldByName('ZBD_FL_JY').AsFloat;
    ZBDFLZBLY:=AUniQuery.FieldByName('ZBD_FL_ZBLY').AsString;
    ZBDFLDJLX:=AUniQuery.FieldByName('ZBD_FL_DJLX').AsString;
  end;  
end;

class procedure TZBDFL.ReadDS(AUniQuery: TUniQuery; var Result: TUniEngine);
begin
  if Result=nil then Exit;
  with TZBDFL(Result) do
  begin
    ZBDFLBH  :=AUniQuery.FieldByName('ZBD_FL_BH').AsInteger;
    ZBDBH    :=AUniQuery.FieldByName('ZBD_BH').AsString;
    ZBDDWBML :=AUniQuery.FieldByName('ZBD_DWBML').AsString;
    ZBDFLZJLX:=AUniQuery.FieldByName('ZBD_FL_ZJLX').AsString;
    ZBDFLZCLX:=AUniQuery.FieldByName('ZBD_FL_ZCLX').AsString;
    ZBDFLBJKM:=AUniQuery.FieldByName('ZBD_FL_BJKM').AsString;
    ZBDFLXJKM:=AUniQuery.FieldByName('ZBD_FL_XJKM').AsString;
    ZBDFLJE  :=AUniQuery.FieldByName('ZBD_FL_JE').AsFloat;
    ZBDFLBZ  :=AUniQuery.FieldByName('ZBD_FL_BZ').AsString;
    ZBDFLJY  :=AUniQuery.FieldByName('ZBD_FL_JY').AsFloat;
    ZBDFLZBLY:=AUniQuery.FieldByName('ZBD_FL_ZBLY').AsString;
    ZBDFLDJLX:=AUniQuery.FieldByName('ZBD_FL_DJLX').AsString;
  end;  
end;

class function  TZBDFL.CopyIt(AZBDFL: TZBDFL): TZBDFL;
begin
  Result:=TZBDFL.Create;
  TZBDFL.CopyIt(AZBDFL,Result)
end;

class procedure TZBDFL.CopyIt(AZBDFL:TZBDFL;var Result:TZBDFL);
begin
  if Result=nil then Exit;
  Result.ZBDFLBH  :=AZBDFL.ZBDFLBH;
  Result.ZBDBH    :=AZBDFL.ZBDBH;
  Result.ZBDDWBML :=AZBDFL.ZBDDWBML;
  Result.ZBDFLZJLX:=AZBDFL.ZBDFLZJLX;
  Result.ZBDFLZCLX:=AZBDFL.ZBDFLZCLX;
  Result.ZBDFLBJKM:=AZBDFL.ZBDFLBJKM;
  Result.ZBDFLXJKM:=AZBDFL.ZBDFLXJKM;
  Result.ZBDFLJE  :=AZBDFL.ZBDFLJE;
  Result.ZBDFLBZ  :=AZBDFL.ZBDFLBZ;
  Result.ZBDFLJY  :=AZBDFL.ZBDFLJY;
  Result.ZBDFLZBLY:=AZBDFL.ZBDFLZBLY;
  Result.ZBDFLDJLX:=AZBDFL.ZBDFLDJLX;
end;

class function TZBDFL.ExpSQL(AUnitLink: string;ADJLX:string): string;
begin
  {Result:='SELECT * FROM TBL_ZBD_FL WHERE EXISTS (SELECT * FROM TBL_ZBD WHERE 1=1'
         +'    AND ZBD_LX=%S AND ZBD_DWBML IN %S AND TBL_ZBD.ZBD_BH=TBL_ZBD_FL.ZBD_BH AND TBL_ZBD.ZBD_DWBML=TBL_ZBD_FL.ZBD_DWBML)'
         +'    AND ZBD_FL_JE <0'
         +'    ORDER BY ZBD_BH,ZBD_FL_BH';}
  Result:='SELECT * FROM TBL_ZBD_FL WHERE EXISTS (SELECT * FROM TBL_ZBD WHERE 1=1 AND ZBD_LX=%S   AND TBL_ZBD.ZBD_BH=TBL_ZBD_FL.ZBD_BH AND  TBL_ZBD.STATE_CODE NOT IN (5,7))'
         +'    AND ZBD_DWBML IN %S'
         +'    AND ZBD_FL_JE <0'
         +'    ORDER BY ZBD_BH,ZBD_FL_BH';

  Result:=Format(Result,[QuotedStr(ADJLX),AUnitLink]);
end;

end.

